package com.motorhome.h5.dao.impl;

import com.motorhome.base.dao.impl.BaseDaoImpl;
import com.motorhome.h5.dao.HomeDao;
import com.motorhome.h5.domain.Car;
import com.motorhome.h5.domain.Order;
import com.motorhome.h5.domain.Price;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * @author: John
 */
@Repository
public class HomeDaoImpl extends BaseDaoImpl implements HomeDao {

    @Override
    public List<Price> getAllPriceList() {
        return query("select id,car_id,price,price_desc,top_vip_rate from m_price", new BeanPropertyRowMapper<>(Price.class));
    }

    @Override
    public List<Car> getAllCarList() {
        return query("select id,name,description,pic_url,view_url,insurance,car_num from m_car where status = 0", new BeanPropertyRowMapper<>(Car.class));
    }

    @Override
    public int insertOrder(Long userId, String phone, String userName, Long startCityId, String startCityName,
                           Long startShopId, String startShopName, Date start, Long endCityId, String endCityName,
                           Long endShopId, String endShopName, Date end, Long carId, String carName, String carDescription,
                           Float price, Integer days, Float total,
                           String totalSnapshot,
                           String couponGuid, String guid, String subCount, String couponValue, String typeName) {
        return update("insert into uc_order(user_id,user_phone,user_name,start_city_id,start_city_name," +
                "start_shop_id,start_shop_name,start_date,end_city_id,end_city_name,end_shop_id,end_shop_name," +
                "end_date,car_id,car_name,car_description,price,days,total,create_time,total_snapshot," +
                        "coupon_guid,guid,sub_count,coupon_value,type_name)" +
                        " values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,now(),?,?,?,?,?,?)",
                userId, phone, userName, startCityId, startCityName, startShopId, startShopName, start,
                endCityId, endCityName, endShopId, endShopName, end, carId, carName, carDescription, price, days, total,
                totalSnapshot,
                couponGuid, guid, subCount, couponValue, typeName);
    }

    @Override
    public Price getPriceByCarId(long carId) {
        return queryForObject("select id,car_id,price_desc,price,rate_weekend,day1,rate1,day2,rate2,day3,rate3" +
                        " from m_price" +
                        " where car_id = ?",
                new BeanPropertyRowMapper<Price>(Price.class), carId);
    }

    @Override
    public Car getCarById(long carId) {
        return queryForObject("select id,name,description,pic_url,view_url,insurance,car_num from m_car where status = 0 and id = ?",
                new BeanPropertyRowMapper<>(Car.class), carId);
    }

    @Override
    public int updateOrderPayStatusSuccess(int payType, long orderId, String payResponse) {
        return update("update uc_order set pay_type = ?, pay_status = 1, pay_response = ? where id = ?",
                payType, payResponse, orderId);
    }

    @Override
    public List<Order> getOrderListByStatus(int status) {
        return query("select id,car_id,start_date,end_date from uc_order where status = ?",
                new BeanPropertyRowMapper<Order>(Order.class), status);
    }

}
